package cn.t.repository.impl.device;

import cn.t.converter.device.DeviceDataConverter;
import cn.t.core.repoistory.RepositoryImpl;
import cn.t.model.device.DeviceDataDO;
import cn.t.persistence.entity.device.DeviceDataPO;
import cn.t.persistence.mapper.device.DeviceDataMapper;
import cn.t.repository.device.DeviceDataRepository;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备盘点数据表(DeviceData)数据仓库
 *
 * @author t
 * @since 2024-05-24 10:18:09
 */
@Slf4j
@Repository
@AllArgsConstructor
public class DeviceDataRepositoryImpl extends RepositoryImpl<DeviceDataMapper, DeviceDataPO, DeviceDataConverter, DeviceDataDO> implements DeviceDataRepository {


    @Override
    public List<DeviceDataDO> findByModel(DeviceDataDO deviceDataDO, int start, Integer pageSize, String field, String direction) {

        String lastSql = getLastSql(start, pageSize, field, direction);

        List<DeviceDataPO> list = getWrapper(deviceDataDO)
                .last(lastSql)
                .list();
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }

        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }

    private static String getLastSql(int start, Integer pageSize, String field, String direction) {
        String lastSql = StringUtils.hasText(field) ?
                " order by " + field + " " + direction + " limit " + start + "," + pageSize
                :
                " limit " + start + "," + pageSize;
        return lastSql;
    }

    private LambdaQueryChainWrapper<DeviceDataPO> getWrapper(DeviceDataDO deviceDataDO) {
        LambdaQueryChainWrapper<DeviceDataPO> wrapper = lambdaQuery();
        if (!ObjectUtils.isEmpty(deviceDataDO.getDeviceId())) {
            wrapper.eq(DeviceDataPO::getDeviceId, deviceDataDO.getDeviceId());
        }
        if (!ObjectUtils.isEmpty(deviceDataDO.getUserId())) {
            wrapper.eq(DeviceDataPO::getUserId, deviceDataDO.getUserId());
        }
        if (!ObjectUtils.isEmpty(deviceDataDO.getParticipantId())) {
            wrapper.eq(DeviceDataPO::getParticipantId, deviceDataDO.getParticipantId());
        }
        if (!ObjectUtils.isEmpty(deviceDataDO.getRequestId())) {
            wrapper.eq(DeviceDataPO::getRequestId, deviceDataDO.getRequestId());
        }
        if (!ObjectUtils.isEmpty(deviceDataDO.getFirmId())) {
            wrapper.eq(DeviceDataPO::getFirmId, deviceDataDO.getFirmId());
        }
        if (!ObjectUtils.isEmpty(deviceDataDO.getDeviceData())) {
            wrapper.eq(DeviceDataPO::getDeviceData, deviceDataDO.getDeviceData());
        }
        if (!ObjectUtils.isEmpty(deviceDataDO.getRecordAt())) {
            wrapper.eq(DeviceDataPO::getRecordAt, deviceDataDO.getRecordAt());
        }
        if (!ObjectUtils.isEmpty(deviceDataDO.getAction())) {
            wrapper.eq(DeviceDataPO::getAction, deviceDataDO.getAction());
        }
        if (!ObjectUtils.isEmpty(deviceDataDO.getBusinessType())) {
            wrapper.eq(DeviceDataPO::getBusinessType, deviceDataDO.getBusinessType());
        }
        if (!ObjectUtils.isEmpty(deviceDataDO.getOrderSn())) {
            wrapper.eq(DeviceDataPO::getOrderSn, deviceDataDO.getOrderSn());
        }
        if (!ObjectUtils.isEmpty(deviceDataDO.getGmtCreatedStart()) && !ObjectUtils.isEmpty(deviceDataDO.getGmtCreatedEnd())) {
            wrapper.between(DeviceDataPO::getRecordAt, deviceDataDO.getGmtCreatedStart(), deviceDataDO.getGmtCreatedEnd());
        }

        return wrapper;
    }

    @Override
    public long count(DeviceDataDO deviceDataDO) {
        return getWrapper(deviceDataDO).count();
    }

    @Override
    public List<DeviceDataDO> queryDeviceDataByTime(DeviceDataDO deviceDataDO) {
        List<DeviceDataPO> list = getWrapper(deviceDataDO).orderByAsc(DeviceDataPO::getOrderSn).orderByAsc(DeviceDataPO::getRecordAt).list();
        if (CollectionUtils.isEmpty(list)){
            return new ArrayList<>();
        }
        return list.stream().map(converter::po2Model).collect(Collectors.toList());
    }
}
